package com.aaa.controller;

import com.aaa.dto.BookCatalogueDto;
import com.aaa.entity.Book;
import com.aaa.entity.FileBook;
import com.aaa.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

import static com.aaa.util.Download.download;

/**
 * @author 杨森
 * @version 1.0
 * @Title: 书籍操作
 * @date 2020/7/24 15:53
 */
@Controller
@Api(value = "书籍管理")
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping("/getBookList")
    @ResponseBody
    @ApiOperation("查询书名")
    public List<Book> getBookList(
            @ApiParam("书名") @RequestParam("key") String key,
            @ApiParam("数据源") @RequestParam("dataSource") String dataSource,
            HttpServletRequest request
    ){
        long start=System.currentTimeMillis();
        List<Book> bookList = bookService.getBookList(key, dataSource, request);
        long end=System.currentTimeMillis();
        System.out.println("本次获取目录共用时"+(end-start)/1000+"s");
        return bookList;
    }

    @PostMapping("/replaceBackgroup")
    @ResponseBody
    @ApiOperation("替换背景")
    public String replaceBackgroup(
            @ApiParam("背景文件") @RequestParam("file") String file
    ){
        try{
            bookService.replaceBackgroup(file);
        }catch (Exception e){
            return "替换失败";
        }
        return "替换成功";
    }



    @PostMapping("/getBookCatalogue")
    @ResponseBody
    @ApiOperation("获取目录")
    public List<BookCatalogueDto> getBookCatalogue(
            @ApiParam("书籍编码") @RequestParam("bookCod") String bookCod,
            HttpServletRequest request
    ){
        long start = System.currentTimeMillis();
        List<BookCatalogueDto> bookCatalogue = bookService.getBookCatalogue(bookCod, request);
        long end = System.currentTimeMillis();
        System.out.println("本次获取目录共用时"+(end-start)/1000+"s");
        return bookCatalogue;
    }
    @RequestMapping("/getBookFile")
    @ResponseBody
    @ApiOperation("获取当前有的书籍")
    public List<FileBook> getBookFile(
        @RequestParam(value = "page",required = false) Integer page
    ){
            if(page==null){
                page=1;
            }
        return bookService.getBookFile(page);
    }

    @PostMapping("/getBookChapter")
    @ResponseBody
    @ApiOperation("获取内容")
    public BookCatalogueDto getBookChapter(
            @ApiParam("书籍编码") @RequestParam("bookCod") String bookCod,
            @ApiParam("章节编码") @RequestParam("chapterCod") String chapterCod,
            HttpServletRequest request
    ){
        return bookService.getBookChapter(bookCod,chapterCod,request);
    }

    @ResponseBody
    @RequestMapping("/downloadBook")
    @ApiOperation("下载书籍")
    public String downloadBook(
            @ApiParam("书籍编码") @RequestParam("bookCod") String bookCod,
            @ApiParam("书籍名称") @RequestParam("bookName1") String bookName,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        try {
            long start = System.currentTimeMillis();
            bookService.downloadBook(bookCod,bookName,request,response);
            long end = System.currentTimeMillis();
            System.out.println("本次下载共耗时"+(end-start)/1000+"s");
            download("/usr/local/webapps/file/"+bookName+".txt",bookName+".txt",request,response);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "下载异常"+e;
        }
    }

}
